const db = require('../db');

class LaborTeamModel {
    // 创建劳务队伍
    static async create(name, leader) {
        const [result] = await db.execute(
            "INSERT INTO labor_team (name, leader, status) VALUES (?, ?, 1)",
            [name, leader]
        );
        const [rows] = await db.execute("SELECT * FROM labor_team WHERE id = ?", [result.insertId]);
        return rows[0];
    }

    // 查询所有（排除软删除）
    static async findAll() {
        const [rows] = await db.execute("SELECT * FROM labor_team WHERE status != 2");
        return rows;
    }

    // 根据 id 查询（排除软删除）
    static async findById(id) {
        const [rows] = await db.execute("SELECT * FROM labor_team WHERE id = ? AND status != 2", [id]);
        return rows.length ? rows[0] : null;
    }

    // 更新劳务队伍（可修改名称、领队、状态）
    static async update(id, name, leader, status = 1) {
        const [result] = await db.execute(
            "UPDATE labor_team SET name = ?, leader = ?, status = ? WHERE id = ? AND status != 2",
            [name, leader, status, id]
        );
        return result.affectedRows > 0;
    }

    // 软删除
    static async delete(id) {
        const [result] = await db.execute("UPDATE labor_team SET status = 2 WHERE id = ?", [id]);
        return result.affectedRows > 0;
    }
}

module.exports = LaborTeamModel;
